package com.fingard.dsp.tcpserver;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptMapping;
import com.fingard.dsp.bank.directConfig.BankConfig;
import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.text.StringHelper;
import com.fingard.util.FtpUtil;
import com.fingard.util.SmbFileUtil;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ThreadPoolReceipt implements Runnable{
    private int taskNum;
    private List<ReceiptMapping> fileList;
    private String bankCode;
    private BankConfig bankConfig;
    private static StringBuilder runLog;
    private static Map<String,ReceiptMapping> map;

    public ThreadPoolReceipt(int num, List<ReceiptMapping> fileList, String bankCode, BankConfig bankConfig) {
        this.taskNum = num;
        this.fileList=fileList;
        this.bankCode=bankCode;
        this.bankConfig=bankConfig;
    }

    @Override
    public void run() {
        if(runLog==null){
            runLog=new StringBuilder();
        }
        runLog.append("*****************正在执行task "+taskNum+"*****************\r\n");
        try {
            for(ReceiptMapping rcptMp:fileList){
                if(map==null){
                    map=new HashMap<String, ReceiptMapping>();
                }
                String ftpFilePath="";
                String fileFlag="";
                String filePath="";
                int result = -1;
                String fileName = rcptMp.srcFile;//回单名
                String localPath=rcptMp.receiptFile;//回单路径
                String tmpByDate = Format.Date8Format.format(new Date());
                if (new File(localPath).exists()) {
                    tmpByDate = tmpByDate.substring(0, 6) + "/" + tmpByDate.substring(6, 8) + "/" + bankCode + "/" + rcptMp.ownAct + "/";
                    if(!"false".equals(FGBiz.dspCenter.dspSet.receiptShareDir) && !"sftp".equals(FGBiz.dspCenter.dspSet.receiptShareDir)){
                        fileFlag="共享目录";
                        result = SmbFileUtil.smbUploading(localPath, tmpByDate, FGBiz.dspCenter.dspSet.receiptShareDir);
                    }else {
                        //ftp/sftp服务器
                        FtpSet ftpSet = FGBiz.dspCenter.dspConfig.getReceiptFtpSet("atsReceipt");
                        //是否需要调整回单文件名
                        if("false".equals(bankConfig.getValueAsString("isEditFileName"))){
                            //WriteBankLogLn("==========文件更换文件名开始==========");
                            String fileSuffix=fileName.substring(fileName.lastIndexOf("."));//.jpg
                            String fileNameNew=new StringBuilder(bankCode).append("_").append(rcptMp.receiptNo).append(fileSuffix).toString();
                            writeBankLogLn("文件"+fileName+"名替换成文件名"+fileNameNew);
                            fileName=fileNameNew;
                        }
                        //是否需要新建上传目录
                        if("false".equals(bankConfig.getValueAsString("isCreateNewFile"))){
                            filePath=fileName;//否
                        }else {
                            filePath=tmpByDate+ fileName;//是
                        }
                        ftpFilePath = StringHelper.trimEndFileSp(ftpSet.upDir) + "/" + filePath;
                        if("sftp".equals(FGBiz.dspCenter.dspSet.receiptShareDir)){
                            fileFlag="sftp服务器";
                            //上传至sftp
                            result = FtpUtil.sftpUploading(ftpSet, localPath, ftpFilePath);
                        }else {
                            fileFlag="ftp服务器";
                            //上传至ftp
                            result = FtpUtil.ftpUploading(ftpSet, localPath, ftpFilePath);
                        }
                    }
                }
                if (result == 0 || result == 1) {
                    if (result == 0) {
                        writeBankLogLn("task"+taskNum+"上传文件"+localPath+"至"+fileFlag+"成功");
                    } else {
                        writeBankLogLn("task"+taskNum+"上传文件"+localPath+"至"+fileFlag+"重复");
                    }
                    String uploadFilePath="";//文件上传路径
                    if(!"false".equals(FGBiz.dspCenter.dspSet.receiptShareDir) && !"sftp".equals(FGBiz.dspCenter.dspSet.receiptShareDir)){
                        uploadFilePath= tmpByDate + fileName;
                    }else {
                        uploadFilePath= filePath;
                    }
                    map.put(uploadFilePath,rcptMp);
                } else {
                    writeBankLogLn("task"+taskNum+"上传文件"+localPath+"至"+fileFlag+"失败，请检查日志");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        writeBankLogLn("*****************task "+taskNum+"执行完毕*****************");
    }

    private void writeBankLogLn(String strlog) {
        runLog.append(Format.DateTimeSSSFormat.format(new Date()) + " " + strlog + "\r\n");
    }

    public static StringBuilder getRunLog() {
        return runLog;
    }

    public static Map<String, ReceiptMapping> getMap() {
        return map;
    }
}
